package com.sfhk.dao.impl;

import com.sfhk.dao.UserDao;
import com.sfhk.domain.User;
import com.sfhk.utils.C3P0Utils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.UUID;

public class UserDaoImpl implements UserDao {
    private JdbcTemplate template = new JdbcTemplate(C3P0Utils.getDateSource());
    @Override
    public User findByUserName(String username) {
        String sql = "select * from user where username = ?";
        User user = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username);
        return user;
    }

    @Override
    public void addUser(User user) {
        UUID uuid = UUID.randomUUID();
        String uid = uuid.toString().replaceAll("\\-", "");
        String sql = "insert into user values(?,?,?,?,?,null,?,?,0,null)";
        template.update(sql,uid,user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getBirthday(),user.getSex());
        System.out.println(user.getUsername());
    }

    public static void main(String[] args) {
        UserDao userDao = new UserDaoImpl();
        User user = userDao.findByUserName("1234");
        System.out.println(user);
    }

}
